package org.visionlibrary.image.filters.edge;

import java.awt.Point;

import javax.media.jai.TiledImage;

import org.visionlibrary.image.acquisition.ImageFactory;
import org.visionlibrary.image.model.AbstractFilter;
import org.visionlibrary.image.model.ConvolutionKernel;
import org.visionlibrary.image.model.KernelFilter;


public class SimpleLaplacian extends AbstractFilter {
	protected float[] mask5 = { -1, -1, -1, -1, -1,
							   -1, -1, -1, -1, -1,
							   -1, -1, 24, -1, -1,
							   -1, -1, -1, -1, -1,
							   -1, -1, -1, -1, -1};

	protected float[] mask3 = { 0, -1, 0,
								-1, 4,-1,
                   			    0, -1, 0};

	private final int size;

	public SimpleLaplacian(){
		this.size = 5;
	}

	public SimpleLaplacian(int size){
		if( size != 3 && size != 5 ){
			throw new IllegalArgumentException("Unsupported size. Only supported size is 3 or 5");
		}
		this.size = size;		
	}
	
	@Override
	public TiledImage applyFilter(TiledImage src, TiledImage dest) {
		if (null == src)
			throw new NullPointerException("Source image is null.");

		if (null == dest)
			dest = (new ImageFactory()).createCompatibleImage(src);
		
		KernelFilter kf = new KernelFilter();
		float[] mask;
		if(size == 3){
			mask = mask3;
		}else{
			mask = mask5;
		}
		int pos = (size-1)/2;
		kf.setKernel(new ConvolutionKernel(size, size, mask, 1, new Point(pos, pos)));
		kf.applyFilter(src, dest);
		
		return dest;
	}

}
